<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang="zh-Hans,zh-tw,en,default">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.3" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.3">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.3" color="#222">





  <meta name="keywords" content="HTTP幂等性,POST,PUT," />





  <link rel="alternate" href="/atom.xml" title="进行中的Full Stack Engineer" type="application/atom+xml" />






<meta name="description" content="1.引子近期项目开发过程中，有个需求是文件断点续传，同时上传数量为5。按照正常上传方法，采用NSOperationQueue作为任务队列，NSOperation作为任务，添加到NSOperationQueue，具体实现后面再写。说说其中发生的奇怪的问题，当一个操作在多次点击暂停上传后，开始上传，终端出现  Nw_connection_write_close 25 connection is not">
<meta name="keywords" content="HTTP幂等性,POST,PUT">
<meta property="og:type" content="article">
<meta property="og:title" content="文件多次上传导致失去连接">
<meta property="og:url" content="https://dearlingzou8789.github.io/2017/11/01/文件多次上传导致失去连接/index.html">
<meta property="og:site_name" content="进行中的Full Stack Engineer">
<meta property="og:description" content="1.引子近期项目开发过程中，有个需求是文件断点续传，同时上传数量为5。按照正常上传方法，采用NSOperationQueue作为任务队列，NSOperation作为任务，添加到NSOperationQueue，具体实现后面再写。说说其中发生的奇怪的问题，当一个操作在多次点击暂停上传后，开始上传，终端出现  Nw_connection_write_close 25 connection is not">
<meta property="og:updated_time" content="2017-11-02T14:17:17.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="文件多次上传导致失去连接">
<meta name="twitter:description" content="1.引子近期项目开发过程中，有个需求是文件断点续传，同时上传数量为5。按照正常上传方法，采用NSOperationQueue作为任务队列，NSOperation作为任务，添加到NSOperationQueue，具体实现后面再写。说说其中发生的奇怪的问题，当一个操作在多次点击暂停上传后，开始上传，终端出现  Nw_connection_write_close 25 connection is not">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    version: '5.1.3',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://dearlingzou8789.github.io/2017/11/01/文件多次上传导致失去连接/"/>





  <title>文件多次上传导致失去连接 | 进行中的Full Stack Engineer</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">进行中的Full Stack Engineer</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">全栈工程师</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://dearlingzou8789.github.io/2017/11/01/文件多次上传导致失去连接/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="XiaoMing">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="进行中的Full Stack Engineer">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">文件多次上传导致失去连接</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-11-01T22:48:02+08:00">
                2017-11-01
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h3 id="1-引子"><a href="#1-引子" class="headerlink" title="1.引子"></a>1.引子</h3><p>近期项目开发过程中，有个需求是文件断点续传，同时上传数量为5。按照正常上传方法，采用NSOperationQueue作为任务队列，NSOperation作为任务，添加到NSOperationQueue，具体实现后面再写。说说其中发生的奇怪的问题，当一个操作在多次点击暂停上传后，开始上传，终端出现</p>
<blockquote>
<p>Nw_connection_write_close 25 connection is not ready,sending error callback</p>
<p>__tcp_connection_write_eof_block_invoke write close callback receive error:[57] socket is not connected</p>
</blockquote>
<p>结果就是不管怎样，重新启动，切换网络环境都不起任何作用，上传一直处于失败状态，即使重新从头开始上传，上传到那一段还是会失败。且报的错是<em><strong>“Domain=NSURLErrorDomain Code=-1005 “The network connection was lost.” “</strong></em>,但其实网络是相通的，接口调用都没有问题。<br><a id="more"></a></p>
<h3 id="2-原因分析"><a href="#2-原因分析" class="headerlink" title="2.原因分析"></a>2.原因分析</h3><p>经过Google,StackOverFlow，baidu后，查阅关键字</p>
<blockquote>
<p>Nw_connection_write_close 25 connection is not ready,sending error callback</p>
</blockquote>
<p>第一次试了修改<em>“<strong>Accept-Encoding:deflate</strong>“</em>，然后在同样测试环境下进行测试，结果和之前一样，未能够修复；第二个方法是让服务器修改<em>“<strong>Keep-Alive:timeout=5, max=100</strong>“</em>,或者更多，由于服务器是另外一个项目，没有权限进行尝试，况且别的项目没有说过这种情况，所以这种方法就不得而知，是否行得通；第三种方法也是我的<a href="https://developer.apple.com/library/content/qa/qa1941/_index.html" target="_blank" rel="external">解决方案</a>，其中有这样一段话:</p>
<blockquote>
<p><strong>For this to work you must make sure that you use the right HTTP method for each request. There are two ways this can go wrong</strong>:</p>
<ol>
<li>If you use a non-idempotent HTTP method for an idempotent request, NSURLSession will not retry the request even though it could safely do so. This isn’t a serious problem; you can just retry the request yourself.</li>
<li>If you use an idempotent HTTP method for a non-idempotent request, that’s very bad. NSURLSession might retry the request even though doing so is unsafe. The only valid solution here is to change your request to use a different HTTP method, which may require server-side changes.</li>
</ol>
</blockquote>
<p>翻译：</p>
<blockquote>
<p><strong>为此，您必须确保为每个请求使用正确的HTTP方法。 有两种方法可能会出错：</strong></p>
<ol>
<li>如果您使用非幂等HTTP方法进行幂等请求，NSURLSession即使可以安全地执行此操作，也不会重试该请求。 这不是一个严重的问题; 您可以自己重试请求。</li>
<li>如果对非幂等请求使用了幂等的HTTP方法，这是非常糟糕的。 NSURLSession可能会重试该请求，即使这样做是不安全的。 唯一有效的解决方案是更改您的请求以使用不同的HTTP方法，这可能需要服务器端更改。</li>
</ol>
</blockquote>
<p>到了现在我才知道HTTP有个幂等性，查阅<a href="https://developer.mozilla.org/zh-CN/docs/Glossary/%E5%B9%82%E7%AD%89" target="_blank" rel="external">资料</a></p>
<blockquote>
<ol>
<li>一个HTTP方法是幂等的，指的是同样的请求被执行一次与连续执行多次的效果是一样的，服务器的状态也是一样的。换句话说就是，幂等方法不应该具有副作用（统计用途除外）。在正确实现的条件下，GET，HEAD，PUT和DELETE 等方法都是幂等的，而 POST 方法不是。所有的 safe 方法也都是幂等的。</li>
<li>幂等性只与后端服务器的实际状态有关，而每一次请求接收到的状态码不一定相同。例如，第一次调用DELETE 方法有可能返回 200，但是后续的请求可能会返回404。DELETE 的言外之意是，开发者不应该使用DELETE方法实现具有删除最后条目功能的 RESTful API。</li>
</ol>
</blockquote>
<p>我上传使用的请求是POST，属于非幂等性，上传中多次点击后再次上传会导致错误的更新。于是我改成幂等性且与POST请求功能一致的PUT请求，PUT是幂等性方法。经过多次实践测试后，没有出现<em><strong>失去连接(“The Connect was Lost”)</strong></em>的提示，并且之前上传失败的连接在修改为PUT请求后也能够继续上传。问题得到解决。</p>
<h3 id="3-后续"><a href="#3-后续" class="headerlink" title="3.后续"></a>3.后续</h3><p>经过这次Bug学习，了解到HTTP幂等性与非幂等性特性。也学习了POST、PUT请求的区别，对于这种有多次交互的请求的方法可以采用PUT请求试试哦！</p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/HTTP幂等性/" rel="tag"># HTTP幂等性</a>
          
            <a href="/tags/POST/" rel="tag"># POST</a>
          
            <a href="/tags/PUT/" rel="tag"># PUT</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/06/18/项目统计行数/" rel="next" title="项目统计行数">
                <i class="fa fa-chevron-left"></i> 项目统计行数
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">XiaoMing</p>
              <p class="site-description motion-element" itemprop="description">爱学习，多思考</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">7</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">14</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          
            <div class="feed-link motion-element">
              <a href="/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          <div class="links-of-author motion-element">
            
          </div>

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-引子"><span class="nav-number">1.</span> <span class="nav-text">1.引子</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-原因分析"><span class="nav-number">2.</span> <span class="nav-text">2.原因分析</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-后续"><span class="nav-number">3.</span> <span class="nav-text">3.后续</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">XiaoMing</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Muse</a> v5.1.3</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.3"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.3"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.3"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.3"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.3"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
